//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        order: {},
        userType: 0,
        orderId: '',
        isClick:0
    },

    onLoad: function (options) {
        var userType = wx.getStorageSync('userType');
        var orderId = options.id;
        this.data.orderId = orderId;
        if(userType){
            this.setData({
                userType: 1,
            })
        }
    },
    onShow: function () {
        this.getOrder(this.data.orderId);
    },
    getOrder: function (orderId) {
        //跑腿的单
        //status  （空是全部  1已经完成 2订单进行中）
        var that = this;
        wx.showToast({
            icon: 'loading',
            title: '加载中'
        })
        app.util.request({
            url: app.globalRequestUrl + '/ordermany',
            method: 'GET',
            data: {
                o_id: orderId || ''
            },
            success: function (res) {
                if (res && (res.msg == 'success' || res.msg == 'successs')) {
                    that.setData({
                        order: res.data
                    })
                }
            },
            complete: () => {
                wx.hideToast()
            }
        })
    },
    bindGetOrder: function (e) {
        //跑腿的单
        //status  （空是全部  1已经完成 2订单进行中）
        var that = this;
        var orderId = e.currentTarget.dataset.id
        wx.showToast({
            icon: 'loading',
            title: '加载中'
        })
        app.util.request({
            url: app.globalRequestUrl + '/comfirm',
            method: 'GET',
            data: {
                o_id: orderId || ''
            },
            success: function (res) {
                if (res && res.msg == 'success') {
                   wx.showToast({
                       title:'接单成功，快去派送吧！',
                       icon:'success'
                   })
                    setTimeout(function () {
                        wx.switchTab({
                            url:'/pages/order/order'
                        })
                    },1500)
                }else if(res.msg == 'have3'){
                    wx.showModal({
                        title:"提示",
                        content:'不要太贪心哦，先去派送手头的单吧！',
                        showCancel:false
                    })
                }else{
                    wx.showModal({
                        title:"提示",
                        content:'晚来了一步，看看别的单吧！',
                        showCancel:false
                    })
                }
            },
            fail:function(){
                wx.showModal({
                    title:"提示",
                    content:'操作失败，请稍后重试',
                    showCancel:false
                })
            },
            complete: () => {
                wx.hideToast()
            }
        })
    },
    //普通用户取消订单
    bindOrderCancel:function (e) {
        var that = this;
        wx.showToast({
            title:'加载中！',
            icon:'loading'
        });
        var id = e.currentTarget.dataset.id;
        var data = {
            o_id:id
        }
        app.util.request({
            url: app.globalRequestUrl + '/setorderno',
            method:'GET',
            data:data,
            success: function (res) { //调起微信支付
                if(res && res.msg == 'success'){
                    wx.showModal({
                        title:'提示',
                        content:'取消订单成功！',
                        showCancel:false
                    });
                    setTimeout(function () {
                        wx.switchTab({
                            url:'/pages/order/order'
                        })
                    },1500)
                }else{
                    wx.showModal({
                        title:'提示',
                        content:'取消订单失败！',
                        showCancel:false
                    });
                    that.getOrder(that.data.orderId)
                }
            },
            fail: function(){
            },
            complete: function(){
                wx.hideToast();
            }
        });
    },
    //普通用户的确认订单，跑腿人员订单里没有操作选项，普通用户有取消（未接单时）和完成
    bindOrderFinish:function (e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        var that = this;
        if(that.data.isClick) return;
        that.data.isClick = 1;
        wx.showToast({
            icon: 'loading',
            title: '加载中'
        })

        app.util.request({
            url:app.globalRequestUrl + '/reseipt',
            method:'GET',
            data:{
                o_id:id
            },
            success:function (res) {
                if(res && res.msg == 'success'){
                    wx.showToast({
                        title:'收货成功！',
                        icon:'success'
                    });
                }
            },
            fail:function(){
                wx.showModal({
                    title:"提示",
                    content:'操作失败，请稍后重试',
                    showCancel:false
                })
            },
            complete:function () {
                wx.hideToast();
                setTimeout(function () {
                    that.data.isClick = 0;
                },1000)
            }
        })
    },
    makePhoneCall:function (e) {
        var phoneNumber = e.currentTarget.dataset.phone;
        if(!phoneNumber) return;
        wx.makePhoneCall({
            phoneNumber: phoneNumber//仅为示例，并非真实的电话号码
        })
    },
    bindBack:function () {
        wx.navigateBack({
            delta: 1
        })
    }
})
